package com.naiterui.ehp.bs.cms.modules.devops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bp.utils.biz.AppJumpProtocol;
import com.naiterui.ehp.bs.cms.modules.devops.config.SelfInspectProperties;
import com.naiterui.ehp.bs.cms.modules.devops.entity.SysSelfInspect;
import com.naiterui.ehp.bs.cms.modules.devops.feign.B2cFeign;
import com.naiterui.ehp.bs.cms.modules.devops.feign.DoctorFeign;
import com.naiterui.ehp.bs.cms.modules.devops.feign.ESignFeign;
import com.naiterui.ehp.bs.cms.modules.devops.feign.PatientFeign;
import com.naiterui.ehp.bs.cms.modules.devops.feign.PushFeign;
import com.naiterui.ehp.bs.cms.modules.devops.feign.VideoConsultFeign;
import com.naiterui.ehp.bs.cms.modules.devops.feign.WeixinFeign;
import com.naiterui.ehp.bs.cms.modules.devops.mapper.SysSelfInspectMapper;
import com.naiterui.ehp.bs.cms.modules.devops.service.SelfInspectService;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectEsignVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectKuaidi100VO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectListVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectPushVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectSccaVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectTrtcVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectTxFaceVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectWxMaVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectWxMchVO;
import com.naiterui.ehp.bs.cms.modules.devops.vo.SelfInspectWxMpVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luao
 * @date 2022/4/26 5:23 PM
 **/
@Service
@RequiredArgsConstructor
public class SelfInspectServiceImpl implements SelfInspectService {

    private final SysSelfInspectMapper sysSelfInspectMapper;

    private final PatientFeign patientFeign;
    private final WeixinFeign weixinFeign;
    private final PushFeign pushFeign;
    private final VideoConsultFeign videoConsultFeign;
    private final DoctorFeign doctorFeign;
    private final ESignFeign eSignFeign;
    private final B2cFeign b2cFeign;

    private final SelfInspectProperties selfInspectProperties;

    @Override
    public List<SelfInspectListVO> list(Integer status) {
        LambdaQueryWrapper<SysSelfInspect> queryWrapper =
                new QueryWrapper<SysSelfInspect>().lambda().eq(status != null, SysSelfInspect::getStatus, status)
                        .eq(SysSelfInspect::getEnable, SysSelfInspect.ENABLE_YES);
        List<SysSelfInspect> sysSelfInspects = sysSelfInspectMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(sysSelfInspects)) {
            return null;
        }
        List<SelfInspectListVO> result = new ArrayList<>();
        return BeanUtil.copyList(sysSelfInspects, result, SelfInspectListVO.class);
    }

    @Override
    public void selfInspectSuccess(Long id) {
        SysSelfInspect sysSelfInspect = sysSelfInspectMapper.selectById(id);
        if (sysSelfInspect == null) {
            return;
        }
        sysSelfInspect.setStatus(SysSelfInspect.STATUS_SUCCESS);
        sysSelfInspectMapper.updateById(sysSelfInspect);
    }

    @Override
    public void selfInspectSuccessByType(Integer type) {
        SysSelfInspect sysSelfInspect = sysSelfInspectMapper
                .selectOne(new QueryWrapper<SysSelfInspect>().lambda().eq(SysSelfInspect::getType, type));
        if (sysSelfInspect == null) {
            return;
        }
        sysSelfInspect.setStatus(SysSelfInspect.STATUS_SUCCESS);
        sysSelfInspectMapper.updateById(sysSelfInspect);
    }

    @Override
    public SelfInspectVO wxMpCheck() throws BusinessException {
        SelfInspectWxMpVO result = SelfInspectWxMpVO.builder().appId(selfInspectProperties.getWxAppId())
                .appSecret(selfInspectProperties.getWxAppSecret()).aseKey(selfInspectProperties.getWxAppAeskey())
                .token(selfInspectProperties.getWxAppToken()).build();
        try {
            patientFeign.wxMpCheck();
            // 自检成功更新自检状态
            this.selfInspectSuccessByType(SysSelfInspect.TYPE_WX_MP);
        } catch (BusinessException e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }

    @Override
    public SelfInspectVO wxMaCheck() throws BusinessException {
        SelfInspectWxMaVO result = SelfInspectWxMaVO.builder().appId(selfInspectProperties.getWxMiniappAppId())
                .appSecret(selfInspectProperties.getWxMiniappAppSecret()).build();
        try {
            weixinFeign.wxMaCheck();
            // 自检成功更新自检状态
            this.selfInspectSuccessByType(SysSelfInspect.TYPE_WX_MA);
        } catch (BusinessException e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }

    @Override
    public SelfInspectVO wxMchCheck() throws BusinessException {
        SelfInspectWxMchVO result = SelfInspectWxMchVO.builder().mchId(selfInspectProperties.getWxMchId())
                .mchKey(selfInspectProperties.getWxMchKey()).mchCertPath(selfInspectProperties.getWxMchCertPath()).build();
        try {
            String qrcode = weixinFeign.miniappQrcode();
            result.setQrCode(qrcode);
        } catch (BusinessException e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }

    @Override
    public SelfInspectVO pushCheck() throws BusinessException {
        SelfInspectPushVO result =
                SelfInspectPushVO.builder().drAndroidAppkey(selfInspectProperties.getUmengDrAndroidAppkey())
                        .drAndroidMastersecret(selfInspectProperties.getUmengDrAndroidMastersecret())
                        .drAndroidPackage(selfInspectProperties.getUmengDrAndroidPackage())
                        .phAndroidAppkey(selfInspectProperties.getUmengPhAndroidAppkey())
                        .phAndroidMastersecret(selfInspectProperties.getUmengPhAndroidMastersecret())
                        .phAndroidPackage(selfInspectProperties.getUmengPhAndroidPackage())
                        .drIosAppkey(selfInspectProperties.getUmengDrIosAppkey())
                        .drIosMastersecret(selfInspectProperties.getUmengDrIosMastersecret())
                        .phIosAppkey(selfInspectProperties.getUmengPhIosAppkey())
                        .phIosMastersecret(selfInspectProperties.getUmengPhIosMastersecret()).build();
        try {
            pushFeign.pushAll("d", "自检测试", AppJumpProtocol.DR_HOME.getK());
        } catch (BusinessException e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }

    @Override
    public SelfInspectVO trtcCheck() throws BusinessException {
        SelfInspectTrtcVO result = SelfInspectTrtcVO.builder().trtcAppid(selfInspectProperties.getTxcloudTrtcAppid())
                .trtcSecretid(selfInspectProperties.getTxcloudTrtcSecretid())
                .trtcSecretkey(selfInspectProperties.getTxcloudTrtcSecretkey())
                .trtcToken(selfInspectProperties.getTxcloudTrtcToken()).build();
        try {
            videoConsultFeign.trtcCheck();
            // 自检成功更新自检状态
            this.selfInspectSuccessByType(SysSelfInspect.TYPE_TENCENT_TRTC);
        } catch (BusinessException e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }

    @Override
    public SelfInspectVO txFaceCheck() throws BusinessException {
        SelfInspectTxFaceVO result =
                SelfInspectTxFaceVO.builder().faceAdAppid(selfInspectProperties.getTxcloudFaceAdAppid())
                        .faceAdAppkey(selfInspectProperties.getTxcloudFaceAdAppkey())
                        .faceAdLicence(selfInspectProperties.getTxcloudFaceAdLicence())
                        .faceSecretid(selfInspectProperties.getTxcloudFaceSecretid())
                        .faceSecretkey(selfInspectProperties.getTxcloudFaceSecretkey())
                        .facePhAppid(selfInspectProperties.getTxcloudFacePhAppid())
                        .facePhAppkey(selfInspectProperties.getTxcloudFacePhAppkey())
                        .facePhLicence(selfInspectProperties.getTxcloudFacePhLicence()).build();
        try {
            doctorFeign.txFaceCheck();
            // 自检成功更新自检状态
            this.selfInspectSuccessByType(SysSelfInspect.TYPE_TENCENT_FACE);
        } catch (BusinessException e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }

    @Override
    public SelfInspectVO esignCheck() throws BusinessException {
        SelfInspectEsignVO result = SelfInspectEsignVO.builder().baseUrl(selfInspectProperties.getEsignWukongBaseUrl())
                .openapiUrl(selfInspectProperties.getEsignWukongOpenapiUrl())
                .projectid(selfInspectProperties.getEsignWukongProjectid())
                .projectsecret(selfInspectProperties.getEsignWukongProjectsecret()).build();
        try {
            eSignFeign.esign();
            // 自检成功更新自检状态
            this.selfInspectSuccessByType(SysSelfInspect.TYPE_ESIGN);
        } catch (BusinessException e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }

    @Override
    public SelfInspectVO sccaCheck() throws BusinessException {
        SelfInspectSccaVO result = SelfInspectSccaVO.builder().apihost(selfInspectProperties.getEsignSccaApihost())
                .appid(selfInspectProperties.getEsignSccaAppid()).appsecret(selfInspectProperties.getEsignSccaAppsecret())
                .useridPrefix(selfInspectProperties.getEsignSccaUseridPrefix()).build();
        try {
            String msg = eSignFeign.scca();
            // 自检成功更新自检状态
            this.selfInspectSuccessByType(SysSelfInspect.TYPE_SCCA);
        } catch (BusinessException e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }

    @Override
    public SelfInspectVO kuaidi100Check(String deliveryId, String companyCode) throws BusinessException {
        SelfInspectKuaidi100VO result = SelfInspectKuaidi100VO.builder().key(selfInspectProperties.getKuaidi100Key())
                .customer(selfInspectProperties.getKuaidi100Customer()).build();
        try {
            b2cFeign.kuaidi100Check(deliveryId, companyCode);
            // 自检成功更新自检状态
            this.selfInspectSuccessByType(SysSelfInspect.TYPE_KUAIDI100);
        } catch (BusinessException e) {
            e.printStackTrace();
            return SelfInspectVO.builder().code(SelfInspectVO.FAIL).data(result).build();
        }
        return SelfInspectVO.builder().code(SelfInspectVO.SUCCESS).data(result).build();
    }
}
